package com.example.onlinetaxi.ui.activity.EditBasicInfoPage;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.IdRes;

import com.baidu.platform.comjni.map.msgcenter.NAMsgCenter;
import com.bigkoo.pickerview.TimePickerView;
import com.example.onlinetaxi.LocalStroage.LocalStorage;
import com.example.onlinetaxi.R;
import com.example.onlinetaxi.callback.IRefreshCallBack;
import com.example.onlinetaxi.dto.DataResponse;
import com.example.onlinetaxi.ui.activity.BaseActivity;
import com.example.onlinetaxi.util.OKHttpUtil.GetDataResponse;
import com.example.onlinetaxi.util.OKHttpUtil.HttpUrl;
import com.example.onlinetaxi.util.OKHttpUtil.OKHttpUtil;
import com.example.onlinetaxi.util.TimeUtil;
import com.example.onlinetaxi.view.Dialog.LoadingDialog;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

// 用来修改个人资料的页面
public class EditBasicInfoActivity extends BaseActivity {

    // 界面刷新接口
    public static IRefreshCallBack iRefreshCallBack;
    // 组件
    @BindView(R.id.mine_cancel_modify)
    TextView mineCancelBtn;
    @BindView(R.id.mine_confirm_modify)
    TextView mineConfirmBtn;
    @BindView(R.id.mine_modify_nickname)
    TextView mineNickname;
    @BindView(R.id.mine_modify_name)
    TextView mineName;
    @BindView(R.id.mine_modify_sex)
    RadioGroup mineSex;
    @BindView(R.id.mine_modify_mail)
    TextView mineMail;
    @BindView(R.id.mine_modify_birthday)
    TextView mineBirthday;
    @BindView(R.id.mine_modify_birthday_picker)
    RelativeLayout mineBirthdayPicker;
    // 时间选择器对象
    private TimePickerView birthdayPicker;

    // 信息
    private String nickname = null;
    private String name = null;
    private String sex = "保密";
    private String birthday = null;
    private String mail = null;

    // 请求参数
    private Map<String, String> map = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_basic_info);
        // 使用@BindView必须要有！
        ButterKnife.bind(this);

        initView();

        initEvent();
    }

    private void initEvent() {
        mineCancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 取消保存，关闭当前界面
                finish();
            }
        });

        mineConfirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 同意保存

                // 弹出加载框
                LoadingDialog.showLoading(EditBasicInfoActivity.this);

                // 获取所有信息
                getUserInput();

                saveUserInfo();

                Toast.makeText(EditBasicInfoActivity.this, "个人信息保存成功！", Toast.LENGTH_SHORT).show();

                finish();

                // 关闭加载框
                LoadingDialog.stopLoading();

                // 保存按钮需要执行接口refresh
                iRefreshCallBack.refresh();
            }
        });

        mineSex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.not_tell:
                        sex = "保密";
                        break;
                    case R.id.male:
                        sex = "男";
                        break;
                    case R.id.female:
                        sex = "女";
                        break;
                }
            }
        });

        mineBirthdayPicker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initTimePicker();
                birthdayPicker.show();
            }
        });

    }

    private void getUserInput() {
        nickname = mineNickname.getText().toString();
        name = mineName.getText().toString();
        birthday = mineBirthday.getText().toString();
        mail = mineMail.getText().toString();
    }

    private void saveUserInfo() {
        map.clear();
        map.put("phone", LocalStorage.phone);
        map.put("nickname", nickname);
        map.put("name", name);
        map.put("sex", sex);
        map.put("birthday", birthday);
        map.put("mail", mail);

        OKHttpUtil.postAsyncRequest(HttpUrl.getBaseUrl(), map, "user", "saveUserBasicInfo");
    }

    private void initView() {
        // 从后端获取数据进行初始化
        getUserInfo();
    }

    private void getUserInfo() {
        map.clear();
        map.put("phone", LocalStorage.phone);

        String request = OKHttpUtil.postAsyncRequest(HttpUrl.getBaseUrl(), map, "user", "getUserBasicInfo");
        DataResponse dataResponse = GetDataResponse.getDataResponse(request);

        mineNickname.setText(dataResponse.getData().get("nickname").toString());
        mineName.setText(dataResponse.getData().get("name").toString());
        String sexStr = dataResponse.getData().get("sex").toString();
        if (sexStr.equals("男")) {
            mineSex.check(R.id.male);
        } else if (sexStr.equals("女")) {
            mineSex.check(R.id.female);
        } else {
            mineSex.check(R.id.not_tell);
        }
        if (dataResponse.getData().get("birthday").toString().equals("")) {
            mineBirthday.setText("请选择您的生日");
        } else {
            mineBirthday.setText(dataResponse.getData().get("birthday").toString());
        }
        mineMail.setText(dataResponse.getData().get("email").toString());

    }

    //初始化时间选择器
    private void initTimePicker() {
        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        startDate.set(1900, 1, 1);//起始时间
        Calendar endDate = Calendar.getInstance();
        endDate.set(2099, 12, 31);//结束时间
        birthdayPicker = new TimePickerView.Builder(this,
                new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        //选中事件回调
                        //mTvMyBirthday 这个组件就是个TextView用来显示日期 如2020-09-08
                        mineBirthday.setText(getTimes(date));
                    }
                })
                //年月日时分秒 的显示与否，不设置则默认全部显示
                .setType(new boolean[]{true, true, true, false, false, false})
                .setLabel("年", "月", "日", "时", "", "")
                .isCenterLabel(true)
                .setDividerColor(Color.DKGRAY)
                .setContentSize(21)
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setDecorView(null)
                .build();
    }

    //格式化时间
    private String getTimes(Date date) {
        if (date != null) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            return format.format(date);
        }
        return "";
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}